package com.example.algorithm.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhy
 * @since : 2022/8/8
 * 二叉树路径拼接
 */
public class AllTreePath {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list=new ArrayList<String>();
        if(root==null){
            return list;
        }
        dfs(root,"",list);
        return list;
    }

    private void dfs(TreeNode root, String path, List<String> list) {
        if(root==null){
            return;
        }
        path+=root.val;
        if(root.right==null&&root.left==null){
            list.add(path);
        }else {
            dfs(root.left,path+"->",list);
            dfs(root.right,path+"->",list);
        }
    }
}
